package com.mxpio.erp.quality.controller;

import com.mxpio.erp.common.enums.NoticeStatusEnums;
import com.mxpio.erp.common.jpa.model.Notice;
import com.mxpio.erp.common.order.policy.NoticeContext;
import com.mxpio.erp.common.order.service.NoticeService;
import com.mxpio.erp.common.quality.entity.InQualityOrder;
import com.mxpio.erp.common.quality.entity.InventoryQualityOrder;
import com.mxpio.erp.common.quality.entity.InventoryQualityOrderLine;
import com.mxpio.erp.common.quality.service.InventoryQualityOrderLineService;
import com.mxpio.erp.common.quality.service.InventoryQualityOrderService;
import com.mxpio.erp.common.vo.OrderExcuteVo;
import com.mxpio.erp.quality.policy.DefaultInQualityOrderNoticePolicy;
import com.mxpio.erp.quality.policy.InventoryQualityOrderNoticePolicy;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "InventoryQualityOrderController", description = "库存质检验收单接口")
@RestController
@RequestMapping("/erp/quality/iqo/")
public class InventoryQualityOrderController {
	
	@Autowired
	private InventoryQualityOrderService service;
	
	@Autowired
	private InventoryQualityOrderLineService lineService;


	@Autowired
	private NoticeService noticeService;

	@GetMapping("list")
	@Operation(summary = "库存质检验收单列表", description = "获取库存质检验收单列表", method = "GET")
	public Result<List<InventoryQualityOrder>> list(Criteria criteria) throws UnsupportedEncodingException {
		List<InventoryQualityOrder> tos = service.list(InventoryQualityOrder.class, criteria);
		return Result.OK(tos);
	}
	
	@GetMapping("page")
	@Operation(summary = "库存质检验收单列表(分页)", description = "获取库存质检验收单列表(分页)", method = "GET")
	public Result<Page<InventoryQualityOrder>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
		Pageable pageAble = PageRequest.of(pageNo-1, pageSize);
		Page<InventoryQualityOrder> page = service.listPage(InventoryQualityOrder.class, pageAble, criteria);
		return Result.OK(page);
	}
	
	@GetMapping("list/{noticeNo}")
	@Operation(summary = "根据noticeNo获取库存质检验收单", description = "根据noticeNo获取库存质检验收单", method = "GET")
	public Result<InventoryQualityOrder> getById(@PathVariable(name = "noticeNo", required = true) String noticeNo) {
		InventoryQualityOrder inQualityOrder = service.getById(InventoryQualityOrder.class, noticeNo);
		return Result.OK(inQualityOrder);
	}
	
	
	
	@DeleteMapping("remove/{noticeNos}")
	@Operation(summary = "删除库存质检验收单", description = "删除库存质检验收单", method = "DELETE")
	public Result<InventoryQualityOrder> remove(@PathVariable(name = "noticeNos", required = true) String noticeNos) {
		String noticeNo[] = noticeNos.split(",");
		for(String key : noticeNo){
			Criteria c = Criteria.create().addCriterion("noticeNo", Operator.EQ, key);
			lineService.removeBatch(InventoryQualityOrderLine.class, c);
			service.delete(InventoryQualityOrder.class, key);
		}
		return Result.OK();
	}
	
    

	/*@GetMapping("listbynos/{noticeNos}")
	@Operation(summary = "根据noticeNos获取对应库存质检验收单及明细", description = "根据noticeNos获取库存质检验收单及明细", method = "GET")
	public Result<List<InventoryQualityOrder>> getByIds(@PathVariable(name = "noticeNos", required = true) String noticeNos) {
		return scrapOutNoticeService.listByIds(noticeNos);
	}*/

	@RequestMapping("submit/{noticeNo}")
	@Operation(summary = "提交库存质检验收单", description = "提交库存质检验收单", method = "POST")
	public Result<Object> submit(@PathVariable(name = "noticeNo", required = true) String noticeNo,
								 @RequestBody OrderExcuteVo orderExcuteVo) {
		InventoryQualityOrder notice = service.getById(InventoryQualityOrder.class, noticeNo);
		if (notice.getNoticeStatus().equals(NoticeStatusEnums.NoticeStatus.CONFIRM.getStatusCode())){
			return Result.error("单据状态已发生变化，已确认状态单据不允许重复确认！");
		}
		NoticeContext context = noticeService.execute(notice, new InventoryQualityOrderNoticePolicy(orderExcuteVo.getLines()));
		if(CollectionUtils.isEmpty(context.getErrors())) {
			return Result.OK();
		}else {
			return Result.error(context.getErrors().get(0));
		}
	}

	@PutMapping("reject/{noticeNo}")
	@Operation(summary = "拒绝库存质检验收单", description = "拒绝库存质检验收单", method = "PUT")
	public Result<? extends Notice> close(@Parameter(description="库存质检验收单单号") @PathVariable(name = "noticeNo", required = true) String noticeNo) {
		InventoryQualityOrder notice = service.getById(InventoryQualityOrder.class, noticeNo);
		return noticeService.updateNoticeStatus(notice, NoticeStatusEnums.NoticeStatus.REJECT);
	}

}
